<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="预约人id/粉丝id" prop="fansId">
      <el-input v-model="dataForm.fansId" placeholder="预约人id/粉丝id"></el-input>
    </el-form-item>
    <el-form-item label="预约网点" prop="deptId">
      <el-select v-model="dataForm.deptId" placeholder="请选择" @change="getSaleDeptList">
        <el-option v-for="item in saleDeptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="预约状态" prop="status">
      <el-select v-model="dataForm.status">
        <el-option :value="1" label="预约成功"></el-option>
        <el-option :value="2" label="处理中"></el-option>
        <el-option :value="3" label="预约失败"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="预约时间" prop="appointTime">
      <el-input v-model="dataForm.appointTime" placeholder="预约时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWxAppointObj, addWxAppointObj, putWxAppointObj} from '@/api/mp/wxappoint'
    import {getDeptInfoByDeptType} from '@/api/admin/dept'

    export default {
    data () {
      return {
        visible: false,
        saleDeptOptions: [],
        dataForm: {
          id: 0,
          fansId: '',
          deptId: '',
          status: '',
          appointTime: '',
          applyTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: ''
        },
        dataRule: {
          fansId: [
            { required: true, message: '预约人id/粉丝id不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '预约部门/预约网点不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '预约状态(1-预约成功,2-处理中,3-预约失败)不能为空', trigger: 'blur' }
          ],
          appointTime: [
            { required: true, message: '预约时间不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxAppointObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxAppointObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxAppointObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
